
/*
 * LoginServlet.java
 *
 * Created on June 14, 2007, 11:59 PM
 */

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.Hashtable;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import User.Uservalidation;
import database.RecordSearch;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import utility.Input;

/**
 *
 * @author Hemant
 */
public class LoginServlet extends HttpServlet
{
	String username;
	GetIdName get;
	/**
	 * 
	 */
	private static final long serialVersionUID = 2058536225948907866L;
	
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    {
        System.out.println("I'm in doPost()");
        doGet(request,response);
    }
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    {
    	HttpSession mysession  = request.getSession(true);
        //System.out.println("I'm in doGet()");
        
        username = request.getParameter("username");
        String password = request.getParameter("password");
        
        //System.out.println(username);
        //System.out.println(password);
        
        PrintWriter out = null;
        
		try
		{
			out = response.getWriter();
		}
		catch (IOException e1) 
		{
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
        
        get = new GetIdName();
        
        Uservalidation validate = new Uservalidation();
        boolean IsUser = validate.validate_User(username,password);
        
        if(IsUser)
        {
            try
            {
                //put username and password in session
                mysession.setAttribute("username",username);
                mysession.setAttribute("password",password);                
                guireportgenerator.RequestHandler requhand = new guireportgenerator.RequestHandler();
                String url = null;
                
                RecordSearch rec = new RecordSearch();
                Hashtable<Object, Object> condition = new Hashtable<Object, Object>();
                condition.put("mid", get.getId(this.getwebpageObject()));
                condition.put("pid", get.getId("home"));
                condition.put("td", "null");
                
                rec.setConditionMap(condition);
                ResultSet rs = rec.getResultSet();
                
                String home = null;
                while(rs.next())
                {
                	home = get.getItem(rs.getString("PV"));
                	if(home.equalsIgnoreCase("no$"))
                		home =  rs.getString("VT");
                }
                
                requhand.setUsername(username);
                requhand.setRequestPage(home);
                requhand.setMaster("home");
                
                //For HTTP
                boolean isURL = requhand.isUrl();    
                
                if(isURL)
                {
                	url = "http://"+Input.URL+requhand.getFile();
                	out.write("<html><head>");
                	out.write("<META HTTP-EQUIV='Refresh' CONTENT='0;URL="+url+">");
                	out.write("</head><body></body></html>");
                	out.flush();
            	}
                else
                {
                	url = requhand.getHtml();
                	out.write(url);
                	out.flush();
                }
                
                /*// For FTP
                url = requhand.getFileContent(requhand.getFileName());
				out.write(url);
				out.flush();*/
            }
            catch (Exception e)
            {
                //e.printStackTrace();
            	try {
					response.sendError(404);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }
            finally
            {
            	out = null;
            }
        }
        else
        {
            try
            {
            	response.sendRedirect("/Login");
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            finally
            {
            	out = null;
            }
        }
    }
    
    public String getwebpageObject()
    {
    	String webpage = get.getId("webpage");
		String userId = get.getId(username);
		
		Hashtable<Object, Object> condition = new Hashtable<Object, Object>();
		condition.put("mid", userId);
		condition.put("pid", webpage);
		condition.put("td", "null");
		
		RecordSearch rs = new RecordSearch();
		rs.setConditionMap(condition);
		ResultSet rs1 = rs.getResultSet();
		String pv = null;
		
		try
    	{
			while(rs1.next())
				pv = get.getItem(rs1.getString("PV"));
			
			rs1.close();
		}
    	catch (Exception e)
		{
			e.printStackTrace();
		}
    	
    	return pv;
    }
}
